package com.xintonghua.bussiness.ui.fragment.client;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.util.Log;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ClientAdapter;
import com.xintonghua.bussiness.adapter.MemberTypeAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ClickTypeBean;
import com.xintonghua.bussiness.bean.MemberBirthdayBean;
import com.xintonghua.bussiness.bean.WarningMemberListBean;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 顾客类别
 * 作者：wanxianyi
 * 时间：2018/3/5.
 * 包名：com.xintonghua.bussiness.ui.fragment.client
 * <p>
 * <p>
 * 完善   liqi
 * 2018-3-29  顾客类别
 */

public class ClientKindActivity extends BaseActivity implements XRecyclerView.LoadingListener, ClientKindType {
    @BindView(R.id.tab_kind)
    TabLayout tabKind;
    @BindView(R.id.lv_kind)
    XRecyclerView lvKind;
    MemberTypeAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_kind);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("顾客类别");
//        for (String str : title) {
//            tabKind.addTab(tabKind.newTab().setText(str));
//        }


        RefreshUtils.initList(this, lvKind, MyUtils.dip2px(this, 4), R.color.grey_bg);
        adapter = new MemberTypeAdapter(ClientKindActivity.this, new ArrayList<ClickTypeBean.ThisTypeBean>());
        lvKind.setAdapter(adapter);
        lvKind.setLoadingListener(this);
        getClientList();
    }

    List<ClickTypeBean> list;


    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne://获取会员生日列表
                try {
                    list = JsonUtil.getListByJsonString(data.toString(), ClickTypeBean.class);

                    if (isFirst)
                        setTabLayout(list);
                    adapter.setList(list.get(tabKind.getSelectedTabPosition()).getThisType());
//                    if (tabWarning.getSelectedTabPosition() == 0) {
//                        adapter.addAll(list.getOneMonth());
//                    } else if (tabWarning.getSelectedTabPosition() == 1) {
//                        adapter.addAll(list.getTwoMonth());
//                    } else if (tabWarning.getSelectedTabPosition() == 2) {
//                        adapter.addAll(list.getThreeMonth());
//                    }
                    adapter.notifyDataSetChanged();
                    lvKind.refreshComplete();
                    lvKind.loadMoreComplete();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    @Override
    public void onRefresh() {
        getClientList();
    }

    @Override
    public void onLoadMore() {
        RefreshUtils.onRefresh(adapter, lvKind);
    }

    @Override
    public void getClientList() {
        HttpCent.getInstance(this).getUserTypeList(ClientKindActivity.this, MyState.requestOne);
    }

    boolean isFirst = true;

    @Override
    public void setTabLayout(final List<ClickTypeBean> list) {
        for (ClickTypeBean str : list) {
            tabKind.addTab(tabKind.newTab().setText(str.getType() + "级"));
        }

        tabKind.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.i("aaa", "position========>" + tab.getPosition() + "///" + tabKind.getSelectedTabPosition());
//                adapter.setMemberType();
                adapter.setList(list.get(tabKind.getSelectedTabPosition()).getThisType());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        isFirst = false;
    }
}

interface ClientKindType {
    void getClientList();//获取顾客列表

    void setTabLayout(List<ClickTypeBean> list);//顾客类别并非写死的
}